<?php
/**
 * Created by PhpStorm.
 * User: XuChengPeng
 * Date: 2016/7/8
 * Time: 10:22
 */
use app\assets\AppAsset;
use yii\widgets\ActiveForm;
use yii\helpers\Html;

AppAsset::register($this);
AppAsset::addCss($this, Yii::$app->request->baseUrl . "/css/city.css");
AppAsset::addScript($this, Yii::$app->request->baseUrl . "/js/area.js");

$this->title = "添加新地址"
?>


<div style="padding-left: 15px;padding-right: 15px;" id="content-wrap">
    <div id="content-left" class="demo">
        <tr>
            <div class="common_div">
                <td><a class="txtInput">收货人</a>
                    <input class="etInput" style="margin-left: 25px" id="etName" type="text" name="fullname"/>
                </td>
            </div>
        </tr>

        <div class="lineDiv"></div>

        <tr>
            <div class="common_div">
                <td>
                    <a class="txtInput">联系电话</a>
                    <input class="etInput" style="margin-left: 10px" name="etPhone" id="etPhone" type="text"/>
                </td>
            </div>
        </tr>

        <div class="lineDiv"></div>

        <div class="common_div">
            <div >
                <a class="txtInput">联系地址</a>
                <select style="margin-left: 10px" id="s_province" name="cho_Province"></select>
            </div>
            <div style="margin-top:10px;margin-left: 74px"><select  id="s_city" name="cho_City" ></select></div>
            <div style="margin-top:10px;margin-left: 74px"><select  id="s_county" name="cho_Area"></select></div>

            <script type="text/javascript">_init_area();</script>
        </div>

        <div class="lineDiv"></div>

        <div class="common_div">
            <textarea id="detailAddress" name="cho_detail" rows="5" placeholder="请输入详细地址"></textarea>
        </div>

        <div align="center" style="margin-top: 19px;padding: 8px;">
            <div class="addDiv" onClick="checkForm()">添加</div>
        </div>
    </div>
    <div id="content-right"></div>
</div>
<script>

    function checkForm() {
        var flag=true;
        var myReg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

        var username = $('input[name=fullname]');
        var phone = $('input[name=etPhone]');
        var detail = $('textarea[name=cho_detail]');

        if(!username.val()){
            alert('请输入姓名');
            return false;
        }else if(!phone.val()){
            alert('请输入手机号');
            return false;
        }else if(!myReg.test(phone.val())){
            alert('请输入有效手机号码');
            return false;
        }else if(!detail.val()){
            alert('请输入详细地址');
            return false;
        }

        var prev = $('select[name=cho_Province]');
        var city = $('select[name=cho_City]');
        var area = $('select[name=cho_Area]');
        var vale = ['省份', '城市', '地区'];
        if (prev.val() == vale[0]) {
            flag=false;
        }
        if (city.val() == vale[1]) {
            flag=false;
        }
        if (area.val() == vale[2]) {
            flag=false;
        }
        if (!flag) {
            alert("信息不完整!");
            return false;
        }

        $.ajax({
            type:"POST",
            dataType:"json",
            data: {
                "fullname":username.val() ,
                "etPhone":phone.val(),
                "cho_Province":prev.val(),
                "cho_City":city.val(),
                "cho_Area":area.val(),
                "cho_detail":detail.val()
            },
            url: "create-address",
            success: function (json) {
                alert("添加成功");
                window.history.back();
            },
            error: function (data) {
                alert("添加失败");
            }
        });

    }

</script>



